CLASS zcl_xlsx_test DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_XLSX_TEST IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.

    DATA:BEGIN OF ls_purchase_orders,
           ebeln    TYPE ebeln,
           ebelp    TYPE ebelp,
           material TYPE matnr,
         END OF ls_purchase_orders.
    DATA lt_purchase_orders LIKE TABLE OF ls_purchase_orders.
    DATA:ls_ztbc_form_temp TYPE ztbc_form_temp.

    DATA attachment_service TYPE REF TO if_attachment_service_api.
    DATA ls_stream TYPE if_attachment_service_api=>ty_s_stream.
    APPEND VALUE #( ebeln = '40000001' ebelp = '000010' material = '400001' )  TO lt_purchase_orders.

    attachment_service = cl_attachment_service_api=>get_instance( ).

    TRY.
        attachment_service->get_list_of_attachments(
          EXPORTING iv_attachmentobjecttype = 'PRODUCT'
                    iv_attachmentobjectkey  = '004101040050045596'
                    iv_technicalobjecttype  = 'BUS1001006'
          IMPORTING et_attachments          = DATA(attachments) ).

      CATCH cx_attachment_service INTO DATA(error).
        DATA(text) = error->get_longtext( ).
    ENDTRY.
    READ TABLE attachments INTO DATA(ls_attachments) INDEX 1.
    TRY.
        attachment_service->get_stream_attachment_content(
          EXPORTING
            iv_attachmentobjecttype = 'PRODUCT'
            iv_technicalobjecttype = 'BUS1001006'
            iv_attachmentobjectkey   = '004101040050045596'
            iv_logicaldocument   = ls_attachments-logicaldocument
            iv_archivedocumentid   = ls_attachments-archivedocumentid
          IMPORTING   es_stream   = ls_stream ).
      CATCH cx_attachment_service.
        "handle exception
    ENDTRY.

    TRY.
*        DATA(lo_read_access) = xco_cp_xlsx=>document->for_file_content( ls_stream-value
*          )->read_access( ).



        DATA(lo_write_access) = xco_cp_xlsx=>document->for_file_content( ls_stream-value )->write_access( ).

        DATA(lo_worksheet) = lo_write_access->get_workbook( )->worksheet->at_position( 1 ).

        DATA(lo_cursor) = lo_worksheet->cursor(
          io_column = xco_cp_xlsx=>coordinate->for_alphabetic_value( 'A' )
          io_row    = xco_cp_xlsx=>coordinate->for_numeric_value( 3 )
        ).

        lo_cursor->get_cell( )->value->write_from( 'EBELN' ).
        lo_cursor->move_right( )->get_cell( )->value->write_from( 'EBELP' ).
        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Material' ).
*        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Net Price' ).
*        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Qty Requested' ).
*        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Percent Complete' ).
*        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Gross Amount' ).
*        lo_cursor->move_right( )->get_cell( )->value->write_from( 'Amount to Accrue' ).


        DATA(lo_selection_pattern) = xco_cp_xlsx_selection=>pattern_builder->simple_from_to(
        )->from_column( xco_cp_xlsx=>coordinate->for_alphabetic_value( 'A' )
        )->from_row( xco_cp_xlsx=>coordinate->for_numeric_value( 4 )
        )->get_pattern( ).

        lo_worksheet->select( lo_selection_pattern
        )->row_stream(
        )->operation->write_from( REF #( lt_purchase_orders )
        )->set_value_transformation( xco_cp_xlsx_write_access=>value_transformation->best_effort
        )->execute( ).

        DATA(lv_file_content) = lo_write_access->get_file_content( ).
        out->write( lv_file_content ).
        ls_ztbc_form_temp-xsd_attachment = lv_file_content.
        ls_ztbc_form_temp-xsd_filename = '11.xlsx'.
        ls_ztbc_form_temp-xsd_mimetype = 'XLSX'.
        MODIFY ztbc_form_temp FROM @ls_ztbc_form_temp.
        COMMIT WORK AND WAIT.
      CATCH cx_root INTO DATA(lx_root).
    ENDTRY.
  ENDMETHOD.
ENDCLASS.
